<?php
namespace Duobao\Controller;

use Duobao\Model\MemberModel as Member;
use Duobao\Model\ShoplistModel as Shoplist;
use Org\Util\JWT as JWT;

class IndexController extends ControllerBase
{

    /**
     * 默认
     */
    public function index()
    {
        echo "<!--hi , i'm from " . MODULE_NAME . "~" . APP_STATUS, "-->";

//       echo parent::getUid(I("get.token"));
//        $msg = JWT::decode('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.ImFhYWEi.JO-jI3CUnEecri0PEAp5soV4bLoKUn83acJXnflJd1E','duobaoapi',array('HS256'));

    }

    /**
     * 首页所有数据
     */
    public function all()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();
        
        try {
            $shopObj = new Shoplist();
            
            $item = $shopObj->getBannerData();
            if ($item) {
                $ret['data']['banner']['item'] = $item;
            } else {
                $ret['data']['banner'] = array();
            }
            
            $item = $shopObj->getLatestAnnouncementData();
            if ($item) {
                $ret['data']['latestAnnouncement']['item'] = $item;
            } else {
                $ret['data']['latestAnnouncement'] = array();
            }
            
            $item = $shopObj->getTimeLimitedBuyingData();
            if ($item) {
                $ret['data']['timeLimitedBuying']['item'] = $item;
            } else {
                $ret['data']['timeLimitedBuying'] = array();
            }
            
            $item = $shopObj->getPopularShopData();
            if ($item) {
                $ret['data']['popular']['item'] = $item;
            } else {
                $ret['data']['popular'] = array();
            }

            #获取上架新品3个商品的信息
            $item = $shopObj->getNewGoods();
            if($item){
                $ret['data']['new']['item'] = $item;
            }else{
                $ret['data']['new'] = array();
            }

        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }
        
        return $this->response($ret);
    }

    /**
     * banner图片
     */
    public function banner()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();
        
        try {
            $shopObj = new Shoplist();
            $item = $shopObj->getBannerData();
            if ($item) {
                $ret['data']['item'] = $item;
            }
        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }
        
        return $this->response($ret);
    }

    /**
     * 限时抢购
     */
    public function timeLimitedBuying()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();
        
        try {
            $shopObj = new Shoplist();
            $item = $shopObj->getTimeLimitedBuyingData();
            if ($item) {
                $ret['data']['item'] = $item;
            }
        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }
        
        return $this->response($ret);
    }

    /**
     * 最新揭晓
     */
    public function latestAnnouncement()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();
        
        try {
            $shopObj = new Shoplist();
            $item = $shopObj->getLatestAnnouncementData();
            if ($item) {
                $ret['data']['item'] = $item;
            }
        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }
        
        return $this->response($ret);
    }

    /**
     * 热门推荐
     */
    public function popularShop()
    {
        $ret = array();
        $ret['status'] = 200;
        $ret['msg'] = '';
        $ret['data'] = array();
        
        try {
            $shopObj = new Shoplist();
            $item = $this->getPopularShopData();
            if ($item) {
                $ret['data']['item'] = $item;
            }
        } catch (\Exception $e) {
            $ret['status'] = $e->getCode();
            $ret['msg'] = $e->getMessage();
        }
        
        return $this->response($ret);
    }
}